package cn.tedu.charging.order.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * mqtt发送工具类，用来给设备发送消息    这俩mqtt只是方便复习所以新建了包mqtt，那应该放在哪个位置？？？
 * 就是把测试类里的发送方法复制过来
 */
@Slf4j
@Component
public class MqttProducer {

    @Autowired
    private MqttClient mqttClient;

    /**
     * 给MQTT消息中间件EMQX发送消息
     */
    public void send(String topic, String message) {
        try {
            log.debug("发送消息到EMQX,topic:{},message:{}", topic, message);
            mqttClient.publish(topic,message.getBytes(),0,false);
            log.debug("发送消息到EMQX,topic:{},message:{},成功", topic, message);
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }

}
